package com.es.elasticsearch.util;

import com.es.elasticsearch.model.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.List;

public class ESMapperUtil {

    /**
     * 新增文档
     * @param indexName
     * @param user
     * @param indexID
     * @return
     * @throws IOException
     */
    public static String addMapper(String  indexName,User user,String indexID) throws IOException {
        RestHighLevelClient client = ESIndexUtil.getESInstance();

        // 新增文档 - 请求对象
        IndexRequest request = new IndexRequest();

        // 设置索引及唯一性标识
        request.index(indexName).id(indexID);

        ObjectMapper objectMapper = new ObjectMapper();
        String productJson = objectMapper.writeValueAsString(user);
        // 添加文档数据，数据格式为 JSON 格式
        request.source(productJson, XContentType.JSON);
        // 客户端发送请求，获取响应对象
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        //3.打印结果信息
        System.out.println("_index:" + response.getIndex());
        System.out.println("_id:" + response.getId());
        System.out.println("_result:" + response.getResult());


        return response.getIndex()+"\n"
                +response.getId()+"\n"
                +response.getResult()+"\n";
    }

    /**
     * 批量新增文档
     * @param indexName
     * @param user
     * @param indexID
     * @return
     * @throws IOException
     */
    public static String addBatchMapper(String  indexName, List<User> user, String indexID) throws IOException {
        RestHighLevelClient client = ESIndexUtil.getESInstance();
        //创建批量新增请求对象
        BulkRequest request = new BulkRequest();


        for(User u:user)
        {
            request.add(new
                    IndexRequest().index(indexName).id(u.getId()).source(XContentType.JSON, "name",
                    u.getName(),"age",u.getAge(),"sex",u.getSex()));
        }



        //客户端发送请求，获取响应对象
        BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
        //打印结果信息
        System.out.println("took:" + responses.getTook());
        System.out.println("items:" + responses.getItems());

        return responses.getTook()+"\n"
                +responses.getItems()+"\n";
    }



    /**
     * 修改文档
     * @param indexName
     * @param user
     * @param indexID
     * @return
     * @throws IOException
     */
    public static String updateMapper(String indexName, User user, String indexID) throws IOException {
        RestHighLevelClient client = ESIndexUtil.getESInstance();
        // 修改文档 - 请求对象
        UpdateRequest request = new UpdateRequest();
        // 配置修改参数
        request.index(indexName).id(indexID);
        // 设置请求体，对数据进行修改
        request.doc(XContentType.JSON, "sex", user.getSex());


        // 客户端发送请求，获取响应对象
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println("_index:" + response.getIndex());
        System.out.println("_id:" + response.getId());
        System.out.println("_result:" + response.getResult());

        return response.getIndex() + "\n"
                + response.getId() + "\n"
                + response.getResult() + "\n";
    }

    /**
     * 查询文档
     * @param indexName
     * @param indexID
     * @return
     * @throws IOException
     */
    public static String queryMapper(String indexName,  String indexID) throws IOException {
        RestHighLevelClient client = ESIndexUtil.getESInstance();

        //1.创建请求对象
        GetRequest request = new GetRequest().index(indexName).id(indexID);

        //2.客户端发送请求，获取响应对象
        GetResponse response = client.get(request, RequestOptions.DEFAULT);

        //3.打印结果信息
        System.out.println("_index:" + response.getIndex());
        System.out.println("_type:" + response.getType());
        System.out.println("_id:" + response.getId());
        System.out.println("source:" + response.getSourceAsString());


        return response.getIndex() + "\n"
                + response.getId() + "\n"
                + response.getType() + "\n"
                + response.getSourceAsString() + "\n";
    }



    /**
     * 删除文档
     * @param indexName
     * @param indexID
     * @return
     * @throws IOException
     */
    public static String deleteMapper(String indexName,  String indexID) throws IOException {
        RestHighLevelClient client = ESIndexUtil.getESInstance();
        //创建请求对象
        DeleteRequest request = new DeleteRequest().index(indexName).id(indexID);
        //客户端发送请求，获取响应对象
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        //打印信息
        System.out.println(response.toString());

        return response.getIndex() + "\n"
                + response.getId() + "\n"
                + response.getType() + "\n";
    }

}
